package com.heakey.app.executor.user.query;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.heakey.app.model.user.req.UserRecordPageReq;
import com.heakey.app.model.user.vo.UserRecordVO;
import com.heakey.common.model.PageResult;
import com.heakey.domain.dao.user.entity.UserRecord;
import com.heakey.domain.model.user.bo.UserRecordBO;
import com.heakey.domain.model.user.dto.UserRecordDTO;
import com.heakey.domain.service.user.IUserRecordService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @Description TODO
 * @Author Bing
 * @Date 2025/6/5 9:30
 */
@RequiredArgsConstructor
@Service
public class UserRecordQueryService {

    private final IUserRecordService userRecordService;

    /**
     * 根据ID查询记录
     * @param id
     * @return
     */
    public UserRecordVO getById(Long id){
        UserRecord userRecord = userRecordService.getById(id);
        if(ObjectUtil.isNull(userRecord)){
            return null;
        }
        return BeanUtil.copyProperties(userRecord, UserRecordVO.class);
    }

    /**
     * 分页查询记录
     * @param pageReq
     * @return
     */
    public PageResult<UserRecordVO> queryPage(UserRecordPageReq pageReq) {
        UserRecordBO param = new UserRecordBO();
        BeanUtil.copyProperties(pageReq, param);
        PageResult<UserRecordDTO> pageResult = userRecordService.queryPage(param, pageReq);
        List<UserRecordDTO> rows = pageResult.getRows();
        if(CollUtil.isEmpty(rows)){
            return PageResult.empty();
        }
        // 列表转换
        List<UserRecordVO> list = BeanUtil.copyToList(rows, UserRecordVO.class);
        return PageResult.success(pageResult.getTotal(),  list);
    }
}